package com.zzptc.zzsgz.bdsjws.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.views.CircleTextImageView;
import com.zzptc.zzsgz.bdsjws.vo.Contact;

import java.util.List;
import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/6/6.
 */

public class ContactAdater extends RecyclerView.Adapter<ContactAdater.MyHoldView>{
    private List<Contact>list;//所有联系人
    private  List<Contact>soslist=MyApplication.getList();
    public ContactAdater( List<Contact>list) {
        this.list=list;
    }

    @Override
    public MyHoldView onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.show_contact_item,parent,false);
        MyHoldView myHoldView=new MyHoldView(view);
        return myHoldView;
    }

    @Override
    public void onBindViewHolder(MyHoldView holder, int position) {
        Contact contact= list.get(position);
        holder.ctiv.setText(contact.getName().substring(1));
        holder.ctiv.setFillColor(000);
        holder.tv_username.setText(contact.getName());
        holder.tv_tel.setText(contact.getPhonenum());
        holder.tv_addr.setText(contact.getAddress());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyHoldView extends RecyclerView.ViewHolder implements  View.OnClickListener {
        @BindView(R.id.ctiv)
        CircleTextImageView ctiv;
        @BindView(R.id.tv_name)
        TextView tv_username;
        @BindView(R.id.tel)
        TextView tv_tel;
        @BindView(R.id.tv_addr)
        TextView tv_addr;
        @BindView(R.id.cb_choose)
        CheckBox cb_choose;
        public MyHoldView(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            int position=getAdapterPosition();
            Contact c=list.get(position);
            String tel=c.getPhonenum().replace(" ","").replace("-","");
          //  Toast.makeText(MyApplication.getContext(),c.getName() +"====", Toast.LENGTH_SHORT).show();
            if (tel.length()==11&&tel.matches("^1[3578]\\d{9}$")) {
                if (cb_choose.isChecked()) {
                    cb_choose.setChecked(false);
                    //count--;
                    soslist.remove(c);
                } else {
                    if (soslist.size()< 3) {
                        cb_choose.setChecked(true);
                        if (!soslist.contains(c))
                        soslist.add(c);
                      //  count++;
                    } else {
                        Toast.makeText(MyApplication.getContext(), "不能超过3个", Toast.LENGTH_SHORT).show();
                    }
                }
            }else {
                Toast.makeText(MyApplication.getContext(), "请选择手机号码", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
